package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.UserFreeze;
import com.tanhua.dubbo.api.UserFreezeApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class UserFreezeApiImpl implements UserFreezeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertUserFreezeDetails(UserFreeze userFreeze) {
        mongoTemplate.insert(userFreeze);
    }

    @Override
    public UserFreeze queryFreezeDetailById(Integer userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        UserFreeze userFreeze = mongoTemplate.findOne(query, UserFreeze.class);
        return userFreeze;
    }

    @Override
    public void update(UserFreeze userFreeze) {
        Query query = new Query(Criteria.where("userId").is(userFreeze.getUserId()));

        Update update = new Update();
        update.set("freezingTime", userFreeze.getFreezingTime());
        update.set("freezingRange", userFreeze.getFreezingRange());
        update.set("reasonsForFreezing", userFreeze.getReasonsForFreezing());
        update.set("frozenRemarks", userFreeze.getFrozenRemarks());
        update.set("reasonsForThawing", userFreeze.getReasonsForThawing());

        mongoTemplate.updateFirst(query, update, UserFreeze.class);
    }

    @Override
    public List<UserFreeze> findAll() {
        List<UserFreeze> userFreezeList = mongoTemplate.findAll(UserFreeze.class);
        return userFreezeList;
    }
}
